package com.xiyou.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyou.weblog.common.domain.dos.ArticleDO;
import com.xiyou.weblog.common.domain.dos.ArticlePublishCountDO;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author xiyou
 * @description 文章模块
 */
public interface ArticleMapper extends BaseMapper<ArticleDO> {

    default Page<ArticleDO> selectPageList(String title,Long current , Long size, LocalDate startDate, LocalDate endDate){
        // 分页对象
        Page<ArticleDO> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<ArticleDO> wrapper = Wrappers.<ArticleDO>lambdaQuery()
                .like(StringUtils.isNotBlank(title), ArticleDO::getTitle, title)
                .ge(Objects.nonNull(startDate),ArticleDO::getCreateTime,startDate) // 大于startTime
                .le(Objects.nonNull(endDate),ArticleDO::getCreateTime,endDate)// 小于结束时间
                .orderByDesc(ArticleDO::getCreateTime);// 按时间倒叙排序

        return selectPage(page, wrapper);
    };

    /**
     * 根据文章 ID 批量分页查询
     */
    default Page<ArticleDO> selectPageListByArticleIds(Long current , Long size, List<Long> articleIds){
        // 分页对象
        Page<ArticleDO> page = new Page<>(current, size);

        //查询条件
        LambdaQueryWrapper<ArticleDO> wrapper = Wrappers.<ArticleDO>lambdaQuery()
                .in(ArticleDO::getId, articleIds) // 批量查询
                .orderByDesc(ArticleDO::getCreateTime);

        return selectPage(page, wrapper);
    }


    /**
     * 查询上一篇文章
     */
    default ArticleDO selectPreArticle(Long articleId){
        return selectOne(Wrappers.<ArticleDO>lambdaQuery()
                .orderByAsc(ArticleDO::getId)// 按文章 ID 升序排列
                .gt(ArticleDO::getId,articleId)// 查询比当前文章 ID 大的
                .last("limit 1")); // 第一条记录即为上一篇文章
    }

    /**
     * 查询下一篇文章
     */
    default ArticleDO selectNextArticle(Long articleId){
        return selectOne(Wrappers.<ArticleDO>lambdaQuery()
                .orderByDesc(ArticleDO::getId)// 按文章 ID 降序排列
                .lt(ArticleDO::getId,articleId)// 查询比当前文章 ID 下的
                .last("limit 1")); // 第一条记录即为上一篇文章
    }

    /**
     * 阅读量+1
     */
    default int increaseReadNum(Long articleId){
        // 执行 SQL : UPDATE t_article SET read_num = read_num + 1 WHERE (id = XX)
        return update(Wrappers.<ArticleDO>lambdaUpdate()
                .setSql("read_num = read_num + 1")
                .eq(ArticleDO::getId, articleId));
    }

    /**
     * 查询所有记录的阅读量
     */
    default List<ArticleDO> selectAllReadNum(){
        // 仅返回阅读量字段
        return selectList(Wrappers.<ArticleDO>lambdaQuery()
                .select(ArticleDO::getReadNum));
    }


    /**
     * 按日分组，并统计每日发布的文章数量
     */
    @Select("SELECT DATE(create_time) AS date, COUNT(*) As count " +
            "FROM t_article " +
            "WHERE create_time >= #{startDate} AND create_time < #{endDate} " +
            "GROUP By DATE(create_time)")
    List<ArticlePublishCountDO> selectDateArticlePublishCount(LocalDate startDate, LocalDate endDate);
}
